{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8c0efe44-5c31-4e3c-b996-2d84570b3f5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hi\n"
     ]
    }
   ],
   "source": [
    "echo hi"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bee17cef-9b9b-4c2e-8288-b02679aed171",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/word-frequency\n",
    "\n",
    "\n",
    "Success!\n",
    "\n",
    "___\n",
    "\n",
    "\n",
    "Runtime\n",
    "158 ms\n",
    "\n",
    "Beats\n",
    "22.88%\n",
    "\n",
    "Memory\n",
    "3.2 MB\n",
    "\n",
    "Beats\n",
    "97.28%\n",
    "\n",
    "___\n",
    "\n",
    "\n",
    "If you could use Python to solve the problem, don't use bash.\n",
    "\n",
    "___\n",
    "\n",
    "\n",
    "```bash\n",
    "#!/bin/bash\n",
    "\n",
    "#2022/12/13 05:32\n",
    "text=`cat words.txt`\n",
    "#echo \"${text}\"\n",
    "\n",
    "new_text=`echo $text | sed -e 's/\\n/ /g'`\n",
    "#echo \"${new_text}\"\n",
    "\n",
    "IFS=' '; splits_list=($new_text); unset IFS\n",
    "\n",
    "declare -A word_map\n",
    "\n",
    "function count_by_using_map() {\n",
    "    # Here $1 is the first parameter, $2 the second, etc.\n",
    "    word=$1;\n",
    "    \n",
    "    if [[ -v \"word_map[$word]\" ]] ; then\n",
    "        word_map[$word]=$((word_map[$word] + 1));\n",
    "    else\n",
    "        word_map[$word]=1;\n",
    "    fi\n",
    "}\n",
    "\n",
    "IFS=' '; # Set space as the delimiter\n",
    "(\n",
    "    for word in \"${splits_list[@]}\";\n",
    "    do\n",
    "        count_by_using_map $word;\n",
    "    done\n",
    "    \n",
    "    for word in \"${!word_map[@]}\"\n",
    "    do\n",
    "        echo \"${word} ${word_map[$word]}\";\n",
    "    done | sort -n -r -k 2\n",
    "    # sort -n -k 2\n",
    ")\n",
    "unset IF\n",
    "#2022/12/13 08:14\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c663efcc-2b9f-42fb-a34d-80788f701d6b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Bash",
   "language": "bash",
   "name": "bash"
  },
  "language_info": {
   "codemirror_mode": "shell",
   "file_extension": ".sh",
   "mimetype": "text/x-sh",
   "name": "bash"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
